package com.sky.controller.admin;

import cn.hutool.core.convert.Convert;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        int i=dishService.sava(dishDTO);
        //思路一： 更新该缓存中分类下的菜品列表
        //思路二：删除该缓存

        return Result.success();
    }
    @GetMapping("/page")
    public Result<PageResult> findPage(DishPageQueryDTO pageQueryDTO){
        PageResult pageResult=dishService.findPage(pageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status")Integer status, Long id){
        dishService.updateStatus(status,id);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteByIds(Integer[] ids){
        List<Integer> list = Convert.toList(Integer.class, ids);
        Integer i = dishService.deleteByIds(list);
        return Result.success(i);
    }

    @GetMapping("{id}")
    public Result<DishVO> findById(@PathVariable("id")Long id){
        DishVO dishVO=dishService.findById(id);
        return Result.success(dishVO);
    }

    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){
        int dto=dishService.updateDish(dishDTO);
        return Result.success(dto);
    }

    @GetMapping("/list")

    @CacheEvict(value = "category_dish",allEntries = true)//删除category——dish组下的所有key
    public Result findDishById(Long categoryId){
        List<DishVO> dishVOS=dishService.findDishById(categoryId);
        return Result.success(dishVOS);
    }
}
